/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.06.18
 *Time:14:14
 */

//实现Cloneable接口
class Test implements Cloneable{
    int test = 10;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
//使用Cloneable接口
class Stu implements Cloneable{
   //成员变量
    String name;
    int age;

   //构造方法
    public Stu(int age, String name) {
        this.age = age;
        this.name = name;
    }

    Test test = new Test();

    //重写父类的clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Stu tem = (Stu) super.clone();
        tem.test = new Test();//重新实例对象赋给test引用
        return tem;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Dog{
    //成员变量
   String name;
   int age;
   //构造方法
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //重写toString方法：在IDEA中鼠标右击找到
    //Generate中toString快速生成
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class A{
    int age;
    String name;
    //重新equals方法
    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;//参数引用为空引用
        if(!(obj instanceof A)) return false;//参数的引用类型与调用方法的引用不同
        //向下转型
        A a = (A) obj;
       return this.name.equals(a);
    }
//    @Override
//    public boolean equals(Object obj) {
//        if(obj == null) return false;//参数引用为空引用
//        if(!(obj instanceof A)) return false;//参数的引用类型与调用方法的引用不同
//        //向下转型
//        A a = (A) obj;
//        return (this.age == a.age);//返回是否相等 真为true 假为false
//    }
}
public class Main{

    public static void main(String[] args) {
        //实例化对象
        A a1 = new A();
        A a2 = new A();
        a1.name = "abc";
        a2.name = "abcd";
        System.out.println(a1.equals(a2));//a1.name != a2.name false
    }

    public static void main3(String[] args) {
        Dog dog = new Dog("旺财",3);
        System.out.println(dog);
    }

    public static void main2(String[] args) throws CloneNotSupportedException{
        //拷贝对象
        Stu stu = new Stu(11,"ss");
        Stu stu1 = (Stu) stu.clone();
        //使用stu1修改test值
        stu1.test.test = 20;//10 -》 20
        //输出test
        System.out.println("stu的test = " + stu.test.test);//10
        System.out.println("stu1的test = " + stu1.test.test);
    }

    public static void main1(String[] args) throws CloneNotSupportedException{
        Stu stu = new Stu(12,"ss");//实例化一个对象
        Stu stu1 = (Stu)stu.clone();//拷贝一份对象，将引用赋给stu1

        stu1.test.test = 20;//将test从10改为20
        //此时stu1中的test引用指向的对象和stu中的test引用指向
        //的是同一个对象，修改后通过引用调用输出的都是20
        System.out.println("stu中的test = " + stu.test.test);//20
        System.out.println("stu1中的test = " + stu1.test.test);//20
    }
}